package com.ruoyi.classai.service.impl;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.classai.domain.EwcAttendanceRecords;
import com.ruoyi.classai.domain.EwcStudent;
import com.ruoyi.classai.domain.dto.CourseAttendanceDto;
import com.ruoyi.classai.domain.dto.EwcStudentDto;
import com.ruoyi.classai.domain.vo.CourseAttendanceVo;
import com.ruoyi.classai.mapper.EwcAttendanceRecordsMapper;
import com.ruoyi.classai.service.IEwcAttendanceRecordsService;
import com.ruoyi.classai.service.IEwcStudentService;
import com.ruoyi.common.core.exception.GlobalException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 考勤记录Service业务层处理
 * 
 * @author lcj
 * @date 2025-02-28
 */
@Service
public class EwcAttendanceRecordsServiceImpl extends ServiceImpl<EwcAttendanceRecordsMapper, EwcAttendanceRecords> implements IEwcAttendanceRecordsService {

    @Autowired
    private IEwcStudentService studentService;

    @Override
    public List<CourseAttendanceVo> getCourseAttendance(String courseId, String chapterId, String classId) {
        List<CourseAttendanceVo> attendanceVoList=new ArrayList<>();
        List<EwcStudentDto> students=studentService.getList(null,classId);
        for (EwcStudentDto student : students) {
            CourseAttendanceVo attendanceVo=new CourseAttendanceVo();
            //查询学生考勤状态
            EwcAttendanceRecords attendanceRecords=this.getOne(Wrappers.<EwcAttendanceRecords>lambdaQuery().eq(EwcAttendanceRecords::getClassId,classId)
                    .eq(EwcAttendanceRecords::getCourseId,courseId)
                    .eq(EwcAttendanceRecords::getChapterId,chapterId)
                    .eq(EwcAttendanceRecords::getStudentId,student.getId()));
            if (Objects.nonNull(attendanceRecords)){//学生已考勤
                attendanceVo.setId(attendanceRecords.getId());
                attendanceVo.setAttendanceStatus(attendanceRecords.getAttendanceStatus());
            }else { //学生未考勤
                if (student.getStatus()==2){ //见习学生
                    //默认添加见习考勤
                    EwcAttendanceRecords jxAttendanceRecords=new EwcAttendanceRecords();
                    jxAttendanceRecords.setStudentId(student.getId());
                    jxAttendanceRecords.setAttendanceDate(DateUtil.date());
                    jxAttendanceRecords.setAttendanceStatus(2);
                    jxAttendanceRecords.setCourseId(courseId);
                    jxAttendanceRecords.setChapterId(chapterId);
                    jxAttendanceRecords.setClassId(classId);
                    this.save(jxAttendanceRecords);
                    attendanceVo.setId(jxAttendanceRecords.getId());
                    attendanceVo.setAttendanceStatus(2);
                }else { //正常学生
                    //默认添加未签到
                    EwcAttendanceRecords jxAttendanceRecords=new EwcAttendanceRecords();
                    jxAttendanceRecords.setStudentId(student.getId());
                    jxAttendanceRecords.setAttendanceDate(DateUtil.date());
                    jxAttendanceRecords.setAttendanceStatus(0);
                    jxAttendanceRecords.setCourseId(courseId);
                    jxAttendanceRecords.setChapterId(chapterId);
                    jxAttendanceRecords.setClassId(classId);
                    this.save(jxAttendanceRecords);
                    attendanceVo.setAttendanceStatus(0);
                    attendanceVo.setId(jxAttendanceRecords.getId());
                }
            }
            attendanceVo.setGender(student.getGender());
            attendanceVo.setStudentId(student.getId());
            attendanceVo.setStudentName(student.getStudentName());
            attendanceVo.setAvatarPath(student.getAvatarPath());
            attendanceVo.setStudentStatus(student.getStatus());
            attendanceVoList.add(attendanceVo);
        }
        return attendanceVoList;
    }

    @Override
    public void addOrDelCourseAttendance(CourseAttendanceDto courseAttendanceDto) {

        //查询是否已经存在记录
        EwcAttendanceRecords checkAttendanceRecords=this.getById(courseAttendanceDto.getId());
        if (Objects.nonNull(checkAttendanceRecords)){
            EwcAttendanceRecords attendanceRecords=new EwcAttendanceRecords();
            attendanceRecords.setId(checkAttendanceRecords.getId());
            attendanceRecords.setAttendanceStatus(courseAttendanceDto.getAttendanceStatus());
            //更新
            this.updateById(attendanceRecords);
        }else {
            throw new GlobalException("没有查询到学生签到记录");
        }
    }
}
